/*
 * Copyright (c) 2008-2010, Petr Panteleyev
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 
 *    1. Redistributions of source code must retain the above copyright notice, 
 *       this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright notice, 
 *       this list of conditions and the following disclaimer in the documentation 
 *       and/or other materials provided with the distribution.
 *    3. The name of the author may not be used to endorse or promote products 
 *       derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR 
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.panteleyev.blogclient;

import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.util.EventObject;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;

import org.panteleyev.swing.UIStrings;
import org.panteleyev.swing.events.FontsUpdatedEvent;
import org.panteleyev.utilities.Typograf;
import org.panteleyev.utilities.events.AppEventListener;

/**
 *
 * @author  Petr Panteleyev
 */
public class TypografFrame extends BloggerFrame {
    private AbstractAction  copyAction;
    private AbstractAction  cutAction;
    private AbstractAction  pasteAction;
    private AbstractAction  typografAction;
    private JTextArea       textEdit;
    private JScrollPane     scroll;
    
    private class ProcessThread extends SwingWorker<Object,Object> {
        private String result;
        
        @Override
        protected Void doInBackground() throws Exception {
            String text = textEdit.getText();
            result = Typograf.process(text);
            return null;
        }

        @Override
        protected void done() {
            textEdit.setText(result);
        }        
    }
    

    /** Creates new form TypografFrame */
    public TypografFrame() {
        createActions();
        
        initComponents();
        createMenuBar();

        addAppEventListener(FontsUpdatedEvent.class, new AppEventListener() {
            @Override
            public void eventReceived(EventObject event) {
                onFontsUpdatedEvent();
            }
        });
    }
    
    private void createActions() {
        int keyMask = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
        
        /* Edit menu */
        copyAction = new BloggingKit.CopyAction();
        cutAction = new BloggingKit.CutAction();
        pasteAction = new BloggingKit.PasteAction();
        
        typografAction = new AbstractAction(L10N.TYPOGRAF_FRAME_TYPOGRAF_ACTION.s(), Icon.EMPTY.i()) {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) { onProcess(); }
        };   
        typografAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_T, keyMask));
    }

    private void initComponents() {
        setTitle(L10N.TYPOGRAF_FRAME_TITLE.s());

        getContentPane().setLayout(new BorderLayout());

        textEdit = new JTextArea(5, 20);
        scroll = new JScrollPane(textEdit);
        textEdit.setLineWrap(true);

        textEdit.setFont(FontManager.getEditorFont());

        getContentPane().add(scroll, BorderLayout.CENTER);

        Options.getBounds(this);
    }
    
    private void createMenuBar() {
        /* File */
        createMenuBar(
            createFileMenu(),
            createMenu(UIStrings.MENU_EDIT_NAME.s(), null,
                cutAction,
                copyAction,
                pasteAction,
                null,
                typografAction
            ),
            createToolsMenu(),
            createWindowMenu(),
            createHelpMenu()
        );
    }
    
    private void onProcess() {
        new ProcessThread().execute();
    }

    private void onFontsUpdatedEvent() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                textEdit.setFont(FontManager.getEditorFont());
            }
        });
    }

}
